﻿
using System.Collections.Generic;
using System.Data;
using System;
using ZHDJ.WebPage;

namespace ZHDJ.MobileInterface
{
    public partial class UploadPartyPhotoPic : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            UploadPartyPhotoPicOutParam result = new UploadPartyPhotoPicOutParam();
            UploadPartyPhotoPicInParam param = new UploadPartyPhotoPicInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            { 
                param = inParam as UploadPartyPhotoPicInParam;
                if (string.IsNullOrEmpty(param.user_id) || param.photos == null || param.photos.Count == 0 || string.IsNullOrEmpty(param.wall_id))
                {
                    result.code = "0";
                    result.desc = "用户ID，留影墙ID，上传照片不能为空";

                    isFlag = false;
                }

                if (isFlag)
                {
                    if (CommonUtils.IsDisableUsers(param.user_id))
                    {
                        result.code = "0";
                        result.desc = "用户状态有问题，请联系管理员";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    isFlag = InterfaceHelper.CheckPartyPhotoPicEx(param.wall_id);
                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "该留影墙不存在";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    foreach (Photo item in param.photos)
                    {
                        if (item.img_name.IndexOf(".") <= 0)
                        {
                            result.code = "0";
                            result.desc = "照片名称不规范，请使用带后缀的照片名称";

                            isFlag = false;
                        }
                    }
                }

                UpFileInfo fileTemp = new UpFileInfo();
                List<UpFileInfo> fileList = new List<UpFileInfo>();
                if (isFlag)
                {
                    foreach (Photo item in param.photos)
                    {
                        //上传文件 
                        fileTemp = new UpFileInfo();
                        //fileID = CommonUtils.UploadImagFileEx(item.img, item.img_name);
                        UpFileResult upResult = UpFileBusiness.Upload(Convert.FromBase64String(item.img), item.img_name, param.user_id, "");

                        if (!upResult.IsSuccess || string.IsNullOrEmpty(upResult.FileID))
                        {
                            result.code = "0";
                            result.desc = "照片上传失败";

                            isFlag = false;
                            break;
                        }

                        fileTemp.fileName = item.img_name;
                        fileTemp.url = upResult.FileID;
                        item.img = "";//照片内容太多，影响数据库性能

                        fileList.Add(fileTemp);
                    }
                }
                else
                {
                    foreach (Photo item in param.photos)
                    {
                        item.img = "";//照片内容太多，影响数据库性能
                    }
                }

                if (isFlag)
                {
                    //保存数据
                    if (fileList.Count == 0)
                    {
                        result.code = "0";
                        result.desc = "没有上传有效相片";

                        isFlag = false;
                    }
                    else
                    {
                        InterfaceHelper.InsertPartyPhotoPicUploadInfo(param.wall_id, fileList, param.user_id);
                    }
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }
              
            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-UploadPartyPhotoPic", SerializeHelper.JsonSerializer<UploadPartyPhotoPicInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<UploadPartyPhotoPicInParam, UploadPartyPhotoPicOutParam>();
            else
                SafeProcessRequest<UploadPartyPhotoPicInParam, UploadPartyPhotoPicOutParam>();
        }
    }

    #region 请求参数
    public class UploadPartyPhotoPicInParam
    {
        public UploadPartyPhotoPicInParam()
        {
            photos = new List<Photo>();
        }

        /// <summary>
        ///	用户名	
        /// </summary>
        public string user_id
        { get; set; }  
    
        /// <summary>
        ///	留影墙ＩＤ
        /// </summary>
        public string wall_id
        { get; set; }

        /// <summary>
        /// 相片列表
        /// </summary>
        public List<Photo> photos
        {
            get;
            set;
        }
    
        ///// <summary>
        /////	照片名称	
        ///// </summary>
        //public string name
        //{ get; set; }  
    
        ///// <summary>
        /////	照片内容 Base64	
        ///// </summary>
        //public string img
        //{ get; set; }   
    }
     
    public class Photo
    {
        /// <summary>
        ///	照片内容		
        /// </summary>
        public string img
        { get; set; }

        /// <summary>
        /// 照片名称	
        /// </summary>
        public string img_name
        { get; set; }

        /// <summary>
        /// 是否首页图片	
        /// </summary>
        public string isfirst
        { get; set; }
    }
    public class UpFileInfo
    {
        public string fileName { get; set; }

        public string url { get; set; }
    }
    #endregion

    #region 结果参数
    public class UploadPartyPhotoPicOutParam
    {
        public UploadPartyPhotoPicOutParam()
        {
            code = "1";
            desc = "操作成功"; 
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; } 
    }  
    #endregion 
}